package util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GetPost <T>{
    public  T getPost(HttpServletRequest req ,Class<T> clazz) throws IOException {
        StringBuffer sb = new StringBuffer();
        String line = null;
        try{
            BufferedReader reader = req.getReader();
            while((line = reader.readLine()) != null){
                sb.append(line);
            }
        }catch(IOException e){
            System.out.println(e);
        }
//        System.out.println(sb);
        return JSON.parseObject(sb.toString(),clazz);
    }
    public Map<String,Object> getMap(HttpServletRequest req){
        Map<String,Object> map = new HashMap<>();
        StringBuffer sb = new StringBuffer();
        String line = null;
        try{
            BufferedReader reader = req.getReader();
            while((line = reader.readLine()) != null){
                sb.append(line);
            }
        }catch(IOException e){
            System.out.println(e);
        }
        map = JSON.parseObject(sb.toString());
        return map;
    }

}
